package com.q.tqb.activity.mine;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.q.tqb.activity.mine.setting.ChangePhoneActivity;
import com.q.tqb.service.AppUpdateService;
import com.q.tqb.R;
import com.q.tqb.activity.HtmlActivity;
import com.q.tqb.activity.mine.setting.SettingBindTelActivity;
import com.q.tqb.activity.mine.setting.SettingPayPassActivity;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.model.AppUpdateInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.SPUtil;
import com.q.tqb.util.VersionUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class MineSettingActivity extends BaseActivity {


    @BindView(R.id.rl_account)
    RelativeLayout mRlAccount;
    @BindView(R.id.rl_version)
    RelativeLayout mRlVersion;
    @BindView(R.id.rl_pay)
    RelativeLayout mRlPay;
    @BindView(R.id.rl_help)
    RelativeLayout mRlHelp;
    @BindView(R.id.rl_abouts)
    RelativeLayout mRlAbouts;
    @BindView(R.id.tv_exit_login)
    TextView mTvExitLogin;
    private ProgressDialog mDialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_mine_setting;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        setTitle(getString(R.string.mine_setting));
    }


    @OnClick({R.id.iv_back, R.id.rl_account, R.id.rl_version, R.id.rl_pay, R.id.rl_help, R.id.rl_abouts, R.id.tv_exit_login})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_account:
                intent = new Intent(MineSettingActivity.this, SettingBindTelActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_version:
                updateApp();
                break;
            case R.id.rl_pay:
                intent = new Intent(MineSettingActivity.this, SettingPayPassActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_help:
                intent = new Intent(MineSettingActivity.this, ChangePhoneActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_abouts:
                intent = new Intent(MineSettingActivity.this, HtmlActivity.class);
                intent.putExtra("title", getString(R.string.mine_about_us));
                intent.putExtra("url", AppConst.URL_ABOUT_US);
                startActivity(intent);
//                intent = new Intent(MineSettingActivity.this, SettingAboutsActivity.class);
//                startActivity(intent);
                break;
            case R.id.tv_exit_login:
                final AlertDialog.Builder normalDialog = new AlertDialog.Builder(MineSettingActivity.this);
                normalDialog.setTitle("温馨提示");
                normalDialog.setMessage("是否确定退出？");
                normalDialog.setPositiveButton(R.string.string_sure,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                SPUtil.clear(MineSettingActivity.this);
                                setResult(RESULT_OK);
                                finish();
                            }
                        });
                normalDialog.setNegativeButton(R.string.string_cancel,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                normalDialog.show();
                break;
            default:
                break;
        }
    }


    private void updateApp() {
        mProgressDialog.show();
        OkHttpUtil.getInstance().post(this, AppConst.URL_GET_APP_VERSION, null, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                mProgressDialog.dismiss();
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                AppUpdateInfo appUpdateInfo = gson.fromJson(element, AppUpdateInfo.class);
                int nowVersion = VersionUtil.getAppVersionName(MineSettingActivity.this);
                if (appUpdateInfo.getVersion() > nowVersion) {
                    showUpdateDialog(appUpdateInfo.getVsersion_msg());
                } else {
                    showToast("当前为APP最新版本");
                }
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                //  showToast(errorMsg);
                mProgressDialog.dismiss();
            }
        });
    }

    private void showUpdateDialog(String message) {

        new AlertDialog.Builder(this)
                .setTitle("版本更新")
                .setMessage(message)
                .setPositiveButton("更新", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        mDialog = new ProgressDialog(MineSettingActivity.this);
                        mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                        mDialog.setCancelable(true);
                        mDialog.setCanceledOnTouchOutside(false);
                        mDialog.setIcon(R.drawable.ic_logo);
                        mDialog.setTitle("版本升级");
                        mDialog.setMax(100);
                        mDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                    }
                                });
                        mDialog.setMessage("正在升级...");
                        mDialog.show();

                        AppUpdateService.startUpdate(MineSettingActivity.this, AppConst.UPDATE_APP, getExternalCacheDir() + "tqb.apk", new AppUpdateService.OnProgressListener() {
                            @Override
                            public void onProgress(int progress) {
                                //更新对话框进度条
                                mDialog.setProgress(progress);
                            }

                            @Override
                            public void onSuccess(boolean isSuccess) {
                                mDialog.dismiss();
                                //失败提示
                                if (!isSuccess) {
                                    showToast("更新不成功");
                                }
                            }
                        });

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .show();
    }
}
